package org.soenea.ass2.services.datasource;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


import org.dsrg.soenea.service.threadLocal.DbRegistry;

public class UserRDG {
	public static final String BASE_NAME = "Users";
	public static final String TABLE = BASE_NAME;
	public static final String INSERT = "INSERT INTO " + TABLE + " (name,age) VALUES (?,?);";
	public static final String UPDATE = "UPDATE " + TABLE + " AS p set p.age=? WHERE p.name=?;";
	public static final String DELETE = "DELETE FROM " + TABLE + " AS p WHERE p.name=?;";
	
	public static final String SELECT_ALL = "SELECT * FROM " + TABLE;
	public static final String SELECT = "SELECT * FROM " + TABLE + " AS p WHERE p.user_name=?;";
	
	public static ArrayList<UserRDG> findAll() throws SQLException {
		ArrayList<UserRDG> result = new ArrayList<UserRDG>();
		PreparedStatement ps = DbRegistry.getDbConnection().prepareStatement(SELECT_ALL);
		ResultSet rs = ps.executeQuery();
		while(rs.next()) {
			result.add(new UserRDG(rs.getString("user_name"), rs.getString("last_name"), rs.getString("first_name"), rs.getString("password"), rs.getInt("id"), rs.getInt("group_id")));
		}
		ps.close();
		return result;
	}
	
	public static UserRDG find(String name) throws SQLException {
		UserRDG result = null;
		PreparedStatement ps = DbRegistry.getDbConnection().prepareStatement(SELECT);
		ps.setString(1, name);
		ResultSet rs = ps.executeQuery();
		if(rs.next()) {
			result = new UserRDG(rs.getString("user_name"), rs.getString("last_name"), rs.getString("first_name"), rs.getString("password"), rs.getInt("id"), rs.getInt("group_id"));
		}
		ps.close();
		return result;
	}
	
	private String first_name,last_name,user_name,password;
	private int user_id,group_id;
	
	public UserRDG(String user_name, String last_name, String first_name, String password, int user_id, int group_id) {
		super();
		this.first_name = first_name;
		this.last_name = last_name;
		this.user_name = user_name;
		this.password = password;
		this.user_id = user_id;
		this.group_id = group_id;
	}
	public int getId() {
		return user_id;
	}
	public void setId(int group_id) {
		this.user_id = group_id;
	}
	public String getName() {
		return user_name;
	}

	public void insert() throws SQLException {
		PreparedStatement ps = DbRegistry.getDbConnection().prepareStatement(INSERT);
		ps.setString(1, user_name);
		ps.setInt(2, user_id);
		ps.execute();
	}

	public void update() throws SQLException {
		PreparedStatement ps = DbRegistry.getDbConnection().prepareStatement(UPDATE);
		ps.setInt(1, user_id);
		ps.setString(2, user_name);
		ps.execute();		
	}

	public void delete() throws SQLException {
		PreparedStatement ps = DbRegistry.getDbConnection().prepareStatement(DELETE);
		ps.setString(1, user_name);
		ps.execute();		
	}

	public String getPassword() {

		return password;
		
	}

}
